package group.raber.auth.service.impl;

import com.google.common.collect.Maps;
import group.raber.auth.entity.Role;
import group.raber.auth.entity.User;
import group.raber.auth.exception.UserNotExistsException;
import group.raber.auth.mapper.RoleMapper;
import group.raber.auth.mapper.UserMapper;
import group.raber.auth.mapper.UserRoleMapper;
import group.raber.auth.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by tisir<yangsong158@qq.com> on 2017-05-13
 */
@Service("authService")
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;


    public User getUser(Long userId) {
        User user = userMapper.selectById(userId);
        if(user!=null)user.setPassword(null);
        return user;
    }

    public User getUser(String userCode) {
        return userMapper.selectByCode(userCode);
    }

    public int saveUser(User user) {
        boolean insertModel = true;
        //有id,则更新
        if(user.getId() != null){
            User exists = getUser(user.getId());
            if(exists != null){//存在,则更新
                insertModel = false;
            }
        }

        user.setPassword(null);//密码不能在保存的时候一起处理
        if(insertModel){
            userMapper.insert(user);
        }else{
            userMapper.update(user);
        }
        return 1;
    }

    public int deleteUser(Long userId) {
        userMapper.deleteById(userId);
        return 1;
    }

    public Map<String, String> getUserProperties(Long userId) {
        return null;
    }

    public int addUserProperty(Long userId, String propertyName, String propertyValue) {
        return 0;
    }

    public int removeUserProperty(Long userId, String propertyName) {
        return 0;
    }

    public boolean verifyPassword(Long userId, String password) {
        User user = userMapper.selectById(userId);
        if(user!=null){
            return password.equals(user.getPassword());
        }else{
            throw new UserNotExistsException("user.id={0}",userId);
        }

    }

    public void updatePassword(Long userId, String password) {
        User user = userMapper.selectById(userId);
        if(user!=null){
            user.setPassword(password);
            userMapper.update(user);
        }else{
            throw new UserNotExistsException("user.id={0}",userId);
        }
    }

    public Role getRole(Long roleId) {
        return roleMapper.selectById(roleId);
    }

    public Role getRole(String roleCode) {
        return roleMapper.selectByCode(roleCode);
    }

    public int saveRole(Role role) {
        boolean insertModel = true;
        //有id,则更新
        if(role.getId() != null){
            Role exists = roleMapper.selectById(role.getId());
            if(exists != null){//存在,则更新
                insertModel = false;
            }
        }

        if(insertModel){
            roleMapper.insert(role);
        }else{
            roleMapper.update(role);
        }
        return 1;
    }

    public int deleteRole(Long roleId) {
        roleMapper.deleteById(roleId);
        return 1;
    }

    public List<Role> getUserRoles(Long userId) {
        return roleMapper.selectRolesByUser(userId);
    }

    public int grantRoleToUser(Long roleId, Long userId) {
        userRoleMapper.delete(userId,roleId);   //先删除可能存在的关联

        Map<String,Object> userRole = Maps.newHashMap();
        userRole.put("id",userRoleMapper.selectMaxId()+1);
        userRole.put("userId",userId);
        userRole.put("roleId",roleId);
        userRoleMapper.insert(userRole);

        return 1;
    }

    public int revokeRoleFormUser(Long roleId, Long userId) {
        userRoleMapper.delete(userId,roleId);
        return 1;
    }

    public boolean userHasRole(Long userId, Long roleId) {
        return userRoleMapper.selectCountByUserIdAndRoleId(userId,roleId)>0;
    }

    public boolean userHasRole(String userCode, String roleCode) {
        return userRoleMapper.selectCountByUserCodeAndRoleCode(userCode,roleCode)>0;
    }

}
